package cn.wolfcode.shop.service.impl;
import java.text.MessageFormat;
import java.util.Date;
import java.math.BigDecimal;

import cn.wolfcode.shop.domain.Product;
import cn.wolfcode.shop.domain.ProductSku;
import cn.wolfcode.shop.domain.UserAddress;
import cn.wolfcode.shop.domain.UserLogin;
import cn.wolfcode.shop.domin.Invoice;
import cn.wolfcode.shop.domin.OrderAction;
import cn.wolfcode.shop.domin.OrderInfo;
import cn.wolfcode.shop.domin.OrderProduct;
import cn.wolfcode.shop.mapper.InvoiceMapper;
import cn.wolfcode.shop.mapper.OrderActionMapper;
import cn.wolfcode.shop.mapper.OrderInfoMapper;
import cn.wolfcode.shop.mapper.OrderProductMapper;
import cn.wolfcode.shop.service.IOrderInfoService;
import cn.wolfcode.shop.service.IProductService;
import cn.wolfcode.shop.service.IProductSkuService;
import cn.wolfcode.shop.service.IUserAddressService;
import cn.wolfcode.shop.util.IdGenerateUtil;
import cn.wolfcode.shop.utils.Assert;
import cn.wolfcode.shop.vo.OrderVo;
import cn.wolfcode.shop.vo.ShopCarVo;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service
public class OrderInfoServiceImpl implements IOrderInfoService {
    @Autowired
    private OrderActionMapper orderActionMapper;
    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Autowired
    private OrderProductMapper orderProductMapper;
    @Autowired
    private InvoiceMapper invoiceMapper;
    @Reference
    private IUserAddressService userAddressService;
    @Reference
    private IProductSkuService productSkuService;
    @Reference
    private IProductService productService;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void createOrder(OrderVo orderVo,UserLogin userLogin) {
        //判断参数
        Assert.notNull(userLogin,"请先登录");

        //获取用户的地址
        Long userAddressId = orderVo.getUserAddressId();
        UserAddress userAddress = userAddressService.selectById(userAddressId);

        //生成订单信息
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOrderSn(IdGenerateUtil.get().nextId()+"");
        orderInfo.setUserId(userLogin.getId());

        orderInfo.setOrderStatus(0);
        orderInfo.setFlowStatus(0);
        orderInfo.setPayStatus(0);
        orderInfo.setOrderTime(new Date());
        orderInfo.setPhone(userAddress.getPhone());
        orderInfo.setConsignee(userAddress.getConsignee());
        orderInfo.setCountry(userAddress.getCountry());
        orderInfo.setProvince(userAddress.getProvince());
        orderInfo.setCity(userAddress.getCity());
        orderInfo.setDistrict(userAddress.getDistrict());
        orderInfo.setAddress(userAddress.getAddress());
        orderInfo.setZipcode(userAddress.getZipcode());

        //支付方式
        orderInfo.setPayType(orderVo.getPay().getPayType());

        orderInfoMapper.insert(orderInfo) ;

        orderInfo.setOrderAmount(new BigDecimal("0"));

        //获取支付类型
        Integer payType = orderVo.getPay().getPayType();


        //获取订单中的商品
        Long[] productSkuIds = orderVo.getProductSkuIds();

        Long key = userLogin.getId();
        //获取缓存中的信息
        for (int i = 0; i < productSkuIds.length; i++) {
            Long productSkuId = productSkuIds[i];
            String minKey = MessageFormat.format("productSkuId:{0}", productSkuId);

            ShopCarVo shopCarVo= (ShopCarVo) redisTemplate.boundHashOps(key).get(minKey);

            Assert.notNull(shopCarVo,"无效的商品");
            ProductSku productSku = productSkuService.selectByProductSkuId(productSkuId);
            Product product = productService.selectByProductId(productSku.getProductId());

            //保存订单商品
            OrderProduct orderProduct = new OrderProduct();
            orderProduct.setOrderId(orderInfo.getId());
            orderProduct.setSkuId(productSkuId);
            orderProduct.setProductName(product.getName());
            orderProduct.setProductNumber(shopCarVo.getNumber());
            orderProduct.setProductPrice(shopCarVo.getPrice());
            orderProduct.setTotalPrice(shopCarVo.getPrice().multiply(new BigDecimal(shopCarVo.getNumber())));
            orderProduct.setProductImg(shopCarVo.getImage());
            orderProduct.setSkuType(shopCarVo.getSkuString());

            orderProductMapper.insert(orderProduct);

            //订单总额
            orderInfo.setOrderAmount(orderInfo.getOrderAmount().add(orderProduct.getTotalPrice()));
            //生成订单,清楚购物车
            redisTemplate.boundHashOps(key).delete(new  Long[]{productSkuId});
        }



        orderInfoMapper.updateByPrimaryKey(orderInfo) ;

        //保存发票相关
        Invoice invoice = orderVo.getInvoice();
        if(invoice != null){
            invoice.setUserId(userLogin.getId());
            invoice.setOrderId(orderInfo.getId());
            invoiceMapper.insert(invoice);
        }

        //生成执行订单信息
        OrderAction orderAction = new OrderAction() ;
        orderAction.setOrderId(orderInfo.getId());
        orderAction.setActionUser(userLogin.getUserName());
        orderAction.setActionPlace("前台");
        orderAction.setActionNote("下单");
        orderAction.setActionTime(new Date());
        orderActionMapper.insert(orderAction) ;


    }
}
